package com.hwatsang.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.lang.StringUtils;

/**
 * A convenience class to generate MD5 string
 * @author reed7
 *
 */
public abstract class MD5 {
	
	public static String getMD5(String source) {
		if(StringUtils.isBlank(source)) {
			return source;
		}
		
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {}
		
		byte[] sourceByte = null;
		String resStr = null;
		try {
			sourceByte = md.digest(source.getBytes("utf-8"));
			byte[] result = md.digest(sourceByte);
			resStr = new String(result, "utf-8");
		} catch (UnsupportedEncodingException e) {}
		
		return resStr;
	}
	
}
